<?php
/**
 * Created by PhpStorm.
 * User: zhongbaitong
 * Date: 2019/11/28
 * Time: 16:03
 */

namespace app\controller;

use sys\Controller;
use sys\Log;

class Index extends Base
{

    /**
     * Index Page for this controller.
     *
     * Maps to the following URL
     *        http://example.com/index.php/index/index
     *    - or -
     *        http://example.com/
     * @see /system/library/Router.php
     *
     * So any other public methods not prefixed with an underscore will
     * map to /index.php/welcome/<method_name>
     * @see https://gitee.com/zhongbaitong/mini-frame.git
     *
     */
    public function index()
    {

        $this->assign("title", "Welcome Page");

        return $this->fetch("", [
            "content" => "hello world .. "
        ]);
    }

    /**
     * get the config value
     * url: http://example.com/index/test_config
     */
    public function test_config()
    {
        # /application/config.php
        $test = config("test");
        # /config/app.php
        $default_timezone = config("app.default_timezone");
        # refer /system/library/Config.php to CRUD config action
        $app = \sys\Config::getInstance()->get("app");

        return json(compact('test', 'default_timezone', "app"));
    }

    /**
     * get the data from mysql
     *
     * before you test this function
     * you should finish the config from '/config/database.php'
     *
     * URL: http://example.com/index.php/index/test_db
     */
    public function test_db()
    {

//        // Db 类继承 Mysql 全部方法
        // 原生查询
//        $rs1 = (new \sys\Db())->getFind("select * from test");
//        // Mysql 继承 Mysqli 类全部方法
//        $rs0 = \sys\Db::query("insert into test set name = 'hello world'");
//        // 补充 db 操作方法到 Mysql 类即可
//        $rs2 = \sys\Mysql::getInstance()->getSelect("select * from test");
//        dump($rs0,$rs1,$rs2);

        // 增
        $data = db("log")->_insert([
            "option_name" => "test key2",
            "option_value" => "test value"
        ]);
        dump($data);
        // or
        $data = db("log")->_insertAll([
            [
                "option_name" => "test key3",
                "option_value" => "test value"
            ],
            [
                "option_name" => "test key4",
                "option_value" => "test value"
            ],
        ]);
        dump($data);

        // 删
        $data = db("log")->_where(["option_id", ">=", 1])->_delete();
        dump($data);

        // 改
        $data = db("log")->_where(["option_id","=",1])->_update([
            "option_name" => "update",
            "option_value" => "update"
        ]);
        dump($data);

        // 查
        $data = db("user")->_alias("u")
            ->_join("fd_user_vip vip","vip.user_id = u.id")
            ->_field("u.id,email,level")
            ->_where("u.id = 1")
            ->_where([
                ["u.status","=",1],
                ["u.create_time",">",time()]
            ],"OR")
            ->_order("u.id desc")
            ->_order("u.create_time ASC")
            ->_page(1,10)
            ->_select();

        // 最新的sql；
        var_dump(db()->_getLastSql());

        return $this->apiReturn(compact("data"));
    }

    /**
     * test get&set the data from redis
     *
     * before you test this function
     * you should open the the redis extension from php.ini
     *
     * URL: http://example.com/index.php/index/test_cache
     */
    public function test_cache()
    {
        $cache = \sys\Cache::getInstance();
        $cache->set("test", "the type of cache is redis ..");
        $data = $cache->get("test");
        return $this->apiReturn(compact("data"));
    }

    /**
     * test log record .
     *
     * you nend to open the permission for runtime ..
     *
     * URL: http://example.com/index.php/index/test_cache
     */
    public function test_log()
    {

        //
        Log::getInstance()->write(
            'Log record in .\runtime\log\\' . date("Ymd") . ".log"
        );

        return "success";
    }

    /**
     * test funciton
     *
     * URL: http://example.com/index.php/index/test_function
     */
    public function test_function()
    {
        /**
         * the dump() comes from /system/funtion.php
         * - and -
         * the test() comes from /application/common.php
         */
        dump(test());

        // apiSuccess comes from /system/library/Controller.php
        return $this->apiSuccess();
    }

}